import request from "@/utils/request";
import qs from "qs";
import { Message, MessageBox } from 'element-ui'

const code = "000000";


export const axiosPostFrom = (url, data) => {
    return request.post(url, qs.stringify(data), {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
        })
        .then(res => {
            if (res.data.code == code) {
              let msg = res.data.msg;
              if (msg !== "") {
                Message({
                  message: msg,
                  type: "success"
                });
              }
            } else {
              Message({
                message: res.data.msg,
                type: "error"
              });
            }
            return res;
        })
        .catch(err => {
            console.error(err);
        })
};

export const axiosGetFrom = (url, data) => {
    return request.get(url,
        {
          params: data,
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          }
        }
      )
      .then(res => {
        if (res.data.code == code) {
          let msg = res.data.msg;
          if (msg !== "") {
            Message({
              message: msg,
              type: "success"
            });
          }
        } else {
          Message({
            message: res.data.msg,
            type: "error"
          });
        }
        return res;
      })
      .catch(err => {
        console.error(err);
      });
};

export const axiosPostJson_select = (url, data) => {
    return request.post(url, data)
      .then(res => {
        if (res.data.code !== code) {
          Message({
            message: res.data.msg,
            type: "error"
          });
        }
        return res;
      })
      .catch(err => {
        console.error(err);
      });
};

export const axiosGetJson_select = (url, data) => {
         return request.get(url, {
             params: data
           })
           .then(res => {
             if (res.data.code !== code) {
                Message({
                  message: res.data.msg,
                  type: "error"
                });
             } 
             return res;
           })
           .catch(err => {
             console.error(err);
           });
       };

export const axiosPostJson = (url, data) => {
    return request.post(url, data)
      .then(res => {
        if (res.data.code == code) {
          let msg = res.data.msg;
          if (msg !== "") {
            Message({
              message: msg,
              type: "success"
            });
          }
        } else {
          Message({
            message: res.data.msg,
            type: "error"
          });
        }
        return res;
      })
      .catch(err => {
        console.error(err);
      });
};

export const axiosGetJson = (url, data) => {
    return request.get(url, {
            params: data
        })
        .then(res => {
            if (res.data.code == code) {
                let msg = res.data.msg;
                if(msg !== ""){
                    Message({
                      message: msg,
                      type: "success"
                    });
                };
            } else {
                Message({
                    message: res.data.msg,
                    type: 'error',
                })
            }
            return res;
        })
        .catch(err => {
            console.error(err);
        })
};

